<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'提现明细'),
	array('op'=>'listday','title'=>'每日提现统计'),
	array('op'=>'listmonth','title'=>'每月提现统计'),
);
if($_RQ['op']=='list'){
	//会员列表
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$fields=array();
	$list=pdo_getslice('core_ideal_money',$params,array($pindex,$psize),$total,$fields,'','money desc,id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=model('core/member')->basic($item['mid']);
			cfc('red_pack')->up_yired($item);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','key'=>'mid','total'=>$total,'psize'=>$psize,'del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'money','title'=>'当前红包余额','type'=>'string'),
			array('name'=>'total_money','title'=>'累计获得红包','type'=>'string'),
			array('name'=>'frozen_money','title'=>'冻结金额','type'=>'string'),
		),'action'=>array(
			array('type'=>'href','op'=>'inlog','title'=>'红包获得日志'),
			array('type'=>'href','op'=>'outlog','title'=>'红包提现日志'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'title'=>'会员ID','name'=>'mid','action'=>'member.get_member/'),
		),
	);
	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='inlog'){
	$mid=intval($_RQ['mid']);
	if(!$mid){
		exi('请先选择会员！','error');
	}
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$mid,
	);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$fields=array();
	$list=pdo_getslice('core_ideal_money_inlog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=model('core/member')->basic($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'inlog','title'=>'获得明细'),
		),
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'money','title'=>'红包金额','type'=>'string'),
			array('name'=>'createtime','title'=>'获得时间','type'=>'time'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
		)),
	);
	exi($return);
}
//提现申请列表
if($_RQ['op']=='outlog'){
	$return['params']=array();
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		//'status'=>0,
	);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$fields=array();
	$list=pdo_getslice('core_ideal_money_outlog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=model('core/member')->basic($item['mid']);
		}
	}
	$return['list']=$list;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','total'=>$total,'del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'money','title'=>'申请提现金额','type'=>'string'),
			array('name'=>'cash_money','title'=>'实际到账金额','type'=>'string'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'time'),
			array('name'=>'to_type','title'=>'提现方式','type'=>'radio','options'=>'to_types'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'order_id','title'=>'提现支付单ID','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>0)),
		)),
		'search'=>array(
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
			array('type'=>'choose','width'=>15,'title'=>'会员ID','name'=>'mid','action'=>'member.get_member/'),
		),
	);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['to_types']=array(
		'1'=>'提现到余额',
		'2'=>'提现到微信零钱',
		'3'=>'提现到支付宝',
		'4'=>'提现到银行卡',
	);
	$return['status']=array(
		'0'=>'待打款',
		'1'=>'已打款',
		'2'=>'已拒绝',
	);
	exi($return);
}
if($_RQ['op']=='listday'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$y=$_RQ['y']?:date('Y');
	$m=$_RQ['m']?:date('m');
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	if(!empty($_RQ['m'])){
		$return['params']['m']=$_RQ['m'];
	}
	$daynum=date("t",strtotime($y.'-'.$m));
	$list=array();
	for($i=1;$i<=$daynum;$i++){
		$starttime=strtotime($y.'-'.$m.'-'.$i);
		$endtime=$starttime+24*3600;
		$params['createtime between']=array($starttime,$endtime);
		$fee=pdo_getsum('core_ideal_money_outlog',$params,'money');
		$list[]=array(
			'title'=>$y.'-'.$m.'-'.$i,
			'fee'=>$fee,
		);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'日期','type'=>'string'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
			array('name'=>'m','width'=>15,'title'=>'月份','type'=>'radio','options'=>'months'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'1'=>'1月',
		'2'=>'2月',
		'3'=>'3月',
		'4'=>'4月',
		'5'=>'5月',
		'6'=>'6月',
		'7'=>'7月',
		'8'=>'8月',
		'9'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='listmonth'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$y=$_RQ['y']?:date('Y');
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	$list=array();
	for($i=1;$i<=12;$i++){
		$starttime=strtotime($y.'-'.$i);
		if($i==12){
			$ny=$y+1;
			$endtime=strtotime($ny.'-01');
		}else{
			$endtime=strtotime($y.'-'.$m+1);
		}
		$params['createtime between']=array($starttime,$endtime);
		$fee=pdo_getsum('core_ideal_money_outlog',$params,'money');
		$list[]=array(
			'title'=>$y.'-'.$m.'-'.$i,
			'fee'=>$fee,
			'addfee'=>$addfee,
		);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'月份','type'=>'string'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$item=pdo_get('core_ideal_money_outlog',array('id'=>$id));
		$type=intval($post['type']);
		$status=intval($post['status']);
		if(!$status){
			exi('请先选择是否审核通过！','error');
		}
		if($status==1){
			model('core/red_pack')->pay_cash($id);
			exi('打款成功！','','outlog');
		}else{
			if(!$post['remark']){
				exi('请填写审核不通过原因！','error');
			}
			model('core/red_pack')->back_cash($id,$post['remark']);
			exi('操作成功！','','outlog');
		}
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('core_ideal_money_outlog',array('id'=>$id));
	if(!$item){
		exi('指定提现申请不存在！','error');
	}
	if($item['status']){
		exi('该提现申请已审核，请勿重复审核~','error');
	}
	
	$return['statuss']=array(
		'1'=>'审核通过并打款',
		'2'=>'审核不通过',
	);
	$return['to_types']=array(
		'1'=>'提现到余额',
		'2'=>'提现到微信零钱',
		'3'=>'提现到支付宝',
		'4'=>'提现到银行卡',
	);
	$item['to_typetitle']=$return['to_types'][$item['to_type']];
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('name'=>'to_typetitle','title'=>'提现方式','type'=>'copy'),
			array('type'=>'copy','name'=>'money','title'=>'提现金额'),
			array('type'=>'copy','name'=>'cash_money','title'=>'真实到账金额'),
			array('type'=>'copy','name'=>'realname','title'=>'姓名','show'=>array('to_type'=>array(3,4))),
			array('type'=>'copy','name'=>'alipay','title'=>'支付宝账号','show'=>array('to_type'=>array(3))),
			array('type'=>'copy','name'=>'bankname','title'=>'所属银行','show'=>array('to_type'=>array(4))),
			array('type'=>'copy','name'=>'bankcard','title'=>'银行卡号','show'=>array('to_type'=>array(4))),
			array('type'=>'radio','name'=>'status','title'=>'审核','options'=>'statuss'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'string','show'=>array('status'=>2)),
		),
	);
	exi($return);
}
if($_RQ['op']=='pay'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要打款的提现申请！','error');
	}
	model('core/red_pack')->pay_cash($id);
	exi('提现成功！');
}